﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace M5.GeminiHybird.Core.BaseForm
{
    public partial class M5FromWin10 : Form
    {
        public M5WebBrowser _chromeBrowser { get; set; }

        private Color _win10Blue = Color.FromArgb(0, 118, 214);

        private Color _win10BlueHover = Color.FromArgb(10, 128, 224);

        private string _initUrl { get; set; }

        public M5FromWin10()
        {
            InitializeComponent();
        }

        public M5FromWin10(string initUrl)
        {
            _initUrl = initUrl;
            InitializeComponent();
        }

        private void M5FromWin10_Load(object sender, EventArgs e)
        {

            if (DesignMode)
                return;

            TitlePanel.BackColor = _win10Blue;

            _chromeBrowser = new M5WebBrowser(_initUrl);
            _chromeBrowser.Dock = DockStyle.Fill;
            _chromeBrowser.Location = new Point(0, 0);
            ContentPanel.Controls.Add(_chromeBrowser);

            lb_Title.Text = this.Text;

            RefreshTitleBtnLocation();
        }

        private void TitleBtn_MouseLeave(object sender, EventArgs e)
        {
            (sender as Label).BackColor = _win10Blue;
        }

        private void TitleBtn_MouseEnter(object sender, EventArgs e)
        {
            (sender as Label).BackColor = _win10BlueHover;
        }

        private void CloseBtn_Click(object sender, EventArgs e)
        {
            //this.Hide();
            this.Close();
            //System.Environment.Exit(0);
        }

        private void MaxBtn_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
                this.WindowState = FormWindowState.Normal;
            else
                this.WindowState = FormWindowState.Maximized;


        }

        private void MiniBtn_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void M5FromNormalStyle_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                if (_chromeBrowser != null)
                {
                    ContentPanel.Refresh();
                }
            }

            RefreshTitleBtnLocation();
        }

        private void RefreshTitleBtnLocation()
        {
            CloseBtn.Left = this.Width - CloseBtn.Width;
            CloseBtn.BackColor = _win10Blue;

            MaxBtn.Left = this.Width - CloseBtn.Width - MaxBtn.Width;
            MaxBtn.BackColor = _win10Blue;

            MiniBtn.Left = this.Width - CloseBtn.Width - MaxBtn.Width - MiniBtn.Width;
            MiniBtn.BackColor = _win10Blue;
        }


        Point MouseDownPoint { get; set; }
        Point WinPoint { get; set; }
        private void TitlePanel_MouseDown(object sender, MouseEventArgs e)
        {
            //Debug.WriteLine(this.Location);
            if (e.Button == MouseButtons.Left)
            {
                WinPoint = this.Location;
                MouseDownPoint = e.Location;
            }
        }

        private void TitlePanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                return;
            }

            //Debug.WriteLine(e.Location);
            if (e.Button == MouseButtons.Left)
            {
                //重新设置rect的位置，跟随鼠标移动
                this.Location = new Point(e.Location.X - MouseDownPoint.X + WinPoint.X, e.Location.Y - MouseDownPoint.Y + WinPoint.Y);
                WinPoint = this.Location;
            }
        }

        private void TitlePanel_MouseUp(object sender, MouseEventArgs e)
        {
            MouseDownPoint = Point.Empty;
        }

        private void TitlePanel_DoubleClick(object sender, EventArgs e)
        {
            MaxBtn_Click(sender, e);
        }
    }
}
